﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using si.caha.Repository;
using si.caha.Model;

namespace si.caha.Repository
{
    public class AddressRepository : BaseRepository
    {
        public AddressRepository(DataContext dataConext) : base(dataConext){}

        public Address Get(int id)
        {
            return GetDataContext().Addresses.FirstOrDefault(a => a.AddressId == id);
        }
        
        public List<Address> GetAll()
        {
            return GetDataContext().Addresses.ToList();
        }

        public List<Address> GetUserAddresses(int userId)
        {
            return GetDataContext().Addresses.Where(a => a.UserId == userId).ToList();
        }

        public Dictionary<string, string> Save(Address entity)
        {
            Dictionary<string, string> errors = ValidateAndGetErrors(entity);
            if (errors.Count == 0)
            {
                GetDataContext().Addresses.Add(entity);
                GetDataContext().SaveChanges();
            }
            return errors;
        }
    }
}